package com.ruoyi.ftrl.io.server;

import com.ruoyi.ftrl.io.server.common.Const;
import com.ruoyi.ftrl.mapper.TemperatureControlMapper;
import com.ruoyi.ftrl.service.MessagePublisher;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.tio.server.TioServer;
import org.tio.server.TioServerConfig;

@Configuration
public class TioConfig {

    public static TioServerConfig tioServerConfig = null;

    @Autowired
    TemperatureControlMapper temperatureControlMapper;

    @Autowired
    MessagePublisher messagePublisher;

    @Bean
    public TioServer tioServer() throws Exception {

        MyServerAioHandler aioHandler = new MyServerAioHandler(temperatureControlMapper, messagePublisher);
        MyServerAioListener serverAioListener = new MyServerAioListener();

        // 创建 TioServerConfig
        tioServerConfig = new TioServerConfig("T-io Server", aioHandler, serverAioListener);
        tioServerConfig.setHeartbeatTimeout(0); // 设置心跳时间

        // 初始化 MQTT 客户端并存入 ChannelContext
        initializeMqttClient(aioHandler);

        // 创建并返回 TioServer
        return new TioServer(tioServerConfig);
    }

    private void initializeMqttClient(MyServerAioHandler aioHandler) {
        try {
            // 创建 MQTT 客户端
            MqttClient mqttClient = new MqttClient("tcp://localhost:1883", "TioClient");
            MqttConnectOptions options = new MqttConnectOptions();
            options.setCleanSession(true);
            mqttClient.connect(options); // 连接到 MQTT 服务器

            // 将 mqttClient 存入 ChannelContext，以便在消息处理中使用
            aioHandler.setMqttClient(mqttClient); // 假设你为 MyServerAioHandler 添加了这个方法
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

    @Bean
    public TioServer wsServerStarter(TioServer tioServer) {
        // 启动 TIO 服务器
        try {
            tioServer.start(Const.SERVER, Const.PORT);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tioServer;
    }
}




//package com.ruoyi.ftrl.io.server;
//import com.ruoyi.ftrl.io.server.common.Const;
//import com.ruoyi.ftrl.mapper.TemperatureControlMapper;
//import com.ruoyi.ftrl.service.MessagePublisher;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.tio.server.TioServer;
//import org.tio.server.TioServerConfig;
//
//@Configuration
//public class TioConfig {
//    //把这个对象置为全局变量
//    public static TioServerConfig tioServerConfig    = null;
//    @Autowired
//    TemperatureControlMapper temperatureControlMapper;
//
//    @Autowired
//    MessagePublisher messagePublisher;
//    @Bean
//    public TioServer tioServer() throws Exception {
//        MyServerAioHandler aioHandler = new MyServerAioHandler(temperatureControlMapper,messagePublisher);
//        MyServerAioListener serverAioListener=new MyServerAioListener();
//        tioServerConfig = new TioServerConfig("T-io Server", aioHandler, serverAioListener);
//        tioServerConfig.setHeartbeatTimeout(0);
//        //Tio.bindBsId();
////     ServerGroupContext serverGroupContext = new ServerGroupContext("TioServer", aioHandler);
//        TioServer tioServer = new TioServer(tioServerConfig);
//
//        return tioServer;
//    }
//
//    @Bean
//    public TioServer wsServerStarter(TioServer tioServer) throws Exception {
//        tioServer.start(Const.SERVER, Const.PORT);
//        return tioServer;
//    }
//}